<template>
  <el-container class="main_con">
    <div class="header">多端设置</div>
    <div class="notice">
      <div class="item" v-if="entry.indexOf(1) > -1">
        <div class="title">微信小程序</div>
        <div class="desc">请先完成基础设置，方可进行其他设置</div>
        <div class="logo"><img :src="require('@/assets/images/icon_mp.png')" /></div>
        <div class="btns">
          <div class="btn" @click="goLink('mpKey')">设置</div>
          <div class="btn" @click="download">下载</div>

        </div>
      </div>

      <div class="item" v-if="entry.indexOf(2) > -1">
        <div class="title">微信公众号</div>
        <div class="desc">请先完成基础设置，方可进行其他设置</div>
        <div class="logo"><img :src="require('@/assets/images/wx.png')" /></div>
        <div class="btns">
          <div class="btn" @click="goLink('wx')">设置</div>
          <div class="btn">
            <el-switch @change="editwx" :active-value="1" :inactive-value="0" v-model="wxa_obj.configuration.is_open" active-text="开启" inactive-text="关闭"></el-switch>
          </div>
          <!-- <div class="btn">预览</div> -->
          <el-popover class="btn" placement="bottom" width="224" trigger="hover" content="">
            <vue-qr :text="url" :size="200"></vue-qr>
            <el-button @click="doCopy(url)" type="text">复制链接</el-button>
            <!-- <el-button type="text" >预览</el-button> -->
            <div slot="reference">预览</div>
          </el-popover>
        </div>
      </div>
      <div class="item" v-if="entry.indexOf(3) > -1">
        <div class="title">H5</div>
        <div class="desc">请先完成基础设置，方可进行其他设置</div>
        <div class="logo"><img :src="require('@/assets/images/h5.png')" /></div>
        <div class="btns">
          <div class="btn">
            <el-switch @change="editConfig" :active-value="1" :inactive-value="0" v-model="obj.configuration.is_open" active-text="开启" inactive-text="关闭"></el-switch>
          </div>
          <el-popover class="btn" placement="bottom" width="224" trigger="hover" content="">
            <vue-qr :text="url" :size="200"></vue-qr>
            <el-button @click="doCopy(url)" type="text">复制链接</el-button>
            <div slot="reference">预览</div>
          </el-popover>
        </div>
      </div>
    </div>
  </el-container>
</template>

<script>
import {wxurlpreview} from '@/api/mall/multiple';
import vueQr from 'vue-qr';
import {mapGetters} from 'vuex';
import {editSiteConfig, getSiteConfig} from '@/api/mall/setting';
export default {
  data() {
    return {
      isLoading: false,
      url: '',

      obj: {
        configuration: {
          is_open: false
        },
        type: 'web'
      },
      wxa_obj: {
        type: 'wx',
        configuration: {
          is_open: false,
          appid: '',
          appsecret: '',
          URL: '1',
          Token: '1',
          EncodingAESKey: '2'
        }
      }
    };
  },
  components: {
    vueQr
  },
  created() {},
  mounted() {
    if (this.entry.indexOf(2) > -1) {
      this.getSiteConfig('wx');
    }
    if (this.entry.indexOf(3) > -1) {
      this.getConfig('web');
    }
    this.wxurlpreview();
  },
  computed: {
    ...mapGetters(['entry'])
  },
  watch: {
    entry() {
      if (this.entry.indexOf(2) > -1) {
        this.getSiteConfig('wx');
      }
      if (this.entry.indexOf(3) > -1) {
        this.getConfig('web');
      }
      this.wxurlpreview();
    }
  },
  methods: {
    //跳转设置
    goLink(type) {
      this.$router.push('/multiple/' + type);
    },
    download(){
      window.location.href='static/mp-weixin.zip'
    },
    //获取站点配置信息
    getSiteConfig(tp) {
      this.isLoading = true;
      getSiteConfig(tp)
        .then(res => {
          this.isLoading = false;
          //微信小程序配置
          if (!res.msg.configuration.licenseFile) {
            res.msg.configuration.licenseFile = '';
          }
          if (res.msg) {
            this.wxa_obj = {...res.msg};
            res.msg.configuration.is_open?this.wxa_obj.configuration.is_open=1:this.wxa_obj.configuration.is_open=0;
          }
          let licenseFile = this.wxa_obj.configuration.licenseFile;
          if (licenseFile) {
            this.fileList = [{name: licenseFile}];
          }
        })
        .catch(e => {
          this.isLoading = false;
        });
    },
    editwx() {
      if (this.wxa_obj.update_time) {
        //修改
        editSiteConfig(this.wxa_obj)
          .then(res => {
            this.isLoading = false;
            //重新加载数据
            this.getSiteConfig('wx');
            if (this.$route.query.isR) {
              this.$router.push('/multiple/mp');
            }
          })
          .catch(e => {
            this.isLoading = false;
          });
      } else {
        //新增
        addSiteConfig(this.wxa_obj)
          .then(res => {
            this.isLoading = false;
            //重新加载数据
            this.getSiteConfig('wx');
            if (this.$route.query.isR) {
              this.$router.push('/multiple/mp');
            }
          })
          .catch(e => {
            this.isLoading = false;
          });
      }
    },
    editConfig() {
      editSiteConfig(this.obj).then(res => {
        this.getConfig('web');
      });
    },
    wxurlpreview() {
      wxurlpreview({}).then(res => {
        console.log(res);
        this.url = res.msg;
      });
    },

    doCopy(url) {
      this.$copyText(url).then(
        res => {
          //this.$alert('复制成功');
          this.$message({
            message: '复制成功',
            type: 'success',
            duration: 2000
          });
        },
        error => {
          this.$alert('无法复制');
        }
      );
    },
    //获取站点配置数据
    getConfig(type) {
      this.isLoading = true;
      getSiteConfig(type)
        .then(res => {
          this.isLoading = false;
          if (res.msg) {
            this.obj = res.msg;
          }
        })
        .catch(e => {
          this.isLoading = false;
        });
    }
  }
};
</script>

<style lang="scss" scoped>
.header {
  background: #e0e3e8;
  height: 40px;
  line-height: 40px;
  padding-left: 20px;
}

.notice {
  display: flex;
  flex-wrap: wrap;

  .item {
    margin: 20px 20px 0 0;
    width: 325px;
    //height: 200px;
    background: #fff;
    text-align: center;

    &:hover {
      cursor: pointer;
      box-shadow: 0 1px 6px rgba(0, 0, 0, 0.2);
    }

    .title {
      margin-top: 20px;
      font-size: 20px;
      font-weight: 800;
    }

    .desc {
      margin-top: 8px;
      font-size: 14px;
      line-height: 24px;
    }

    .logo {
      margin: 40px 0;
      img {
        width: 84px;
        height: 84px;
      }
    }

    .btns {
      display: flex;
      border-top: 1px solid #ebeef5;
      height: 50px;
      line-height: 50px;

      .btn {
        flex-grow: 1;
        border-right: 1px solid #ebeef5;

        :last-child {
          border-right: 0;
        }

        &:hover {
          background-color: #f2f6fc;
        }
      }
    }
  }
}
</style>
